<?php

namespace app\api\model;

use app\api\job\Message;
use app\api\job\SendMessage;
use think\Model;

class RtcWheat extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:抢麦
     * User: tangfen
     * Date: 2023/12/21
     * Time: 16:14
     */
    public function addWheat($params)
    {
        $res = RtcWheat::where(['channel_id' => $params['channel_id'], 'uid' => $params['uid'], 'status' => 1])->value('id');//存在排队中的用户status=1
        if (empty($res)) {
            //不存在群时添加记录
            $this->data($params);
            $result = $this->save();
            if ($result) {
                return $this->id;
            } else {
                return false;
            }
        } else {
            return_ajax(0, '该用户已在抢麦列表');
        }
    }

    /**
     * Notes:抢麦列表
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:34
     */
    public function WheatList($params)
    {
        $where = [
            'rw.channel_id' => $params['channel_id'],
            'rw.status' => 1, //1排队中，2退出排队
        ];
        $list = $this::alias('rw')
            ->join('user u', 'u.id=rw.uid', 'LEFT')
            ->where($where)
            ->field('u.id as uid ,u.nick,rw.channel_id,rw.wheat_end_time,rw.create_time,rw.id')
            ->order('rw.create_time asc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return $list;
    }


    /**
     * Notes:下麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 21:06
     * @param $params
     * @return bool|mixed
     */
    public function delWheat($params)
    {
        $res = RtcWheat::where(['channel_id' => $params['channel_id'], 'uid' => $params['uid']])->update(['status' => 2]);//1排队中，2退出排队(已退出）
        if ($res !== false) {
            return true;
        } else {
            return_ajax(0, '下麦失败');
        }
    }

    /**
     * Notes:抢麦第一个用户开始发言
     * User: tangfen
     * Date: 2023/12/28
     * Time: 21:16
     */
    public function startSpeak($params)
    {
        $result = $this::where(['channel_id' => $params['channel_id'], 'uid' => $params['uid'], 'status' => 1])->field('status,wheat_end_time,update_time,create_time,is_many_speak')->find();//判断是否可以发言
        if (empty($result)) {//1排队中，2退出排队(已退出或者到规定结束时间）
            return_ajax(0, '已退出或者到规定结束时间');
        }
        if ($result['is_many_speak'] == 2) {//刚刚讲话

            $wheat_duration = RtcAnchor::where('channel_id', $params['channel_id'])->value('wheat_duration');//房间设置允许发言时长(秒)
            $wheatEndTime = $wheat_duration + time();
            $res = RtcWheat::where(['channel_id' => $params['channel_id'], 'uid' => $params['uid']])->update(['wheat_end_time' => $wheatEndTime, 'is_many_speak' => 1]);//上麦记录到期时间

            $update_time = strtotime($result['update_time']);
        } else {
            //暂停后再发言 is_many_speak=1
            $wheat_duration = RtcAnchor::where('channel_id', $params['channel_id'])->value('wheat_duration');//房间设置的上面时长(秒)
            $useTime = time() - strtotime($result['create_time']);
            $wheat_duration = $wheat_duration - $useTime +$params['waiting_time'];
            $wheatEndTime = time() + $wheat_duration+ $params['waiting_time'];

//            var_dump($useTime);
//            var_dump($wheatEndTime);
            $update_time = time();

            $res = RtcWheat::where(['channel_id' => $params['channel_id'], 'uid' => $params['uid']])->update(['wheat_end_time' => $wheatEndTime, 'update_time' => $update_time]);

        }

        $data = [
            'job_queue_name' => 'removeSpeakWheat',
            'channel_id' => $params['channel_id'],
            'uid' => $params['uid'],
            'where_time' => $update_time,
            'wheat_duration' => $wheat_duration
        ];

        //加入延时队列   (自动更新抢麦用户发言状态)
        \app\api\job\SendMessage::sendMessage($data);

        if ($res !== false) {
            return true;
        } else {
            return_ajax(0, '抢麦第一个用户开始发言失败');
        }
    }


    /**
     * Notes:队列自动更新抢麦用户发言状态
     * User: tangfen
     * Date: 2023/12/29
     * Time: 12:56
     */
    public static function removeSpeakWheat($data)
    {
        return true;
//        $res = RtcWheat::where(['channel_id' => $data['channel_id'], 'uid' => $data['uid'],'update_time'=>$data['where_time']])->update(['status' => 2]);//2退出排队
//        if ($res !== false) {
//            return true;
//        }
//        return false;
    }


}